package com.emep.changzhi.analyse.service.information;

import com.emep.changzhi.analyse.entity.information.SysPromptInfo;
import com.emep.changzhi.analyse.repository.information.SysPromptInfoRepository;
import com.emep.changzhi.analyse.utils.MapUtils;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author Wangcc
 * @version V1.0
 * @Description: 报警规则信息业务层
 * @date 2018/2/26 17:02
 */
@Service
@Slf4j
public class SysPromptInfoService {
    
    
    @Resource
    private SysPromptInfoRepository sysPromptInfoRepository;


    /**
     *
     * 不含查询条件的分页获取数据
     *
     * @param pageNo
     * @param pageSize
     * @return
     * @throws Exception
     */
    public Page<SysPromptInfo> findAll(Integer pageNo, Integer pageSize){
        try {
            Pageable pageable = new PageRequest(pageNo-1, pageSize);
            return  sysPromptInfoRepository.findAll(pageable);
        } catch (Exception e) {
            log.error(e.getLocalizedMessage());
            return null;
        }
    }

    /**
     *
     * 新增 报警规则信息
     *
     * @param payload
     * @return
     * @throws Exception
     */
    public SysPromptInfo save(Map<String, Object> payload) {
        SysPromptInfo SysPromptInfo = null;
        try {
            SysPromptInfo SysPromptInfo1 = (SysPromptInfo) MapUtils.convertMap(SysPromptInfo.class,payload);
            SysPromptInfo = sysPromptInfoRepository.save(SysPromptInfo1);
        }catch (Exception e){
            log.error(e.getLocalizedMessage());
        }
        return SysPromptInfo;
    }


    /**
     *
     * 根据id查询 报警规则信息
     *
     * @param id
     * @return
     */
    public SysPromptInfo findId(String id){
        SysPromptInfo SysPromptInfo = null;
        try {
            SysPromptInfo = sysPromptInfoRepository.findSysPromptInfoById(id);
        }catch (Exception e){
            log.error(e.getLocalizedMessage());
        }
        return SysPromptInfo;
    }

    /**
     *
     * 根据id 删除 报警规则信息
     *
     * @param id
     */
    public Boolean delete(String id) {
        Boolean result = false;
        try {
            Integer result1 = sysPromptInfoRepository.deleteById(id);
            if(result1 == 1){
                result = true;
            }
        }catch (Exception e){
            log.error(e.getLocalizedMessage());
        }
        return result;
    }

    /**
     *查询所有 报警规则信息
     *
     * @return List<SysPromptInfo>
     */
    public List<SysPromptInfo> findAll() {
        List<SysPromptInfo> SysPromptInfos = null;
        try {
            SysPromptInfos =  sysPromptInfoRepository.findAll();
        }catch (Exception e){
            log.error(e.getLocalizedMessage());
        }
        return SysPromptInfos;
    }
}
